namespace :bld do
  desc "stat current"
  task :stat => :environment do
    puts time = Time.now
    results = {}
    current_weekly = Weekly.last
    pre_weekly =  current_weekly.last
    #当前期集计投稿数与增值
    results[:current_work_count] = current_weekly.weekly_results.size
    results[:added_work_count] = current_weekly.weekly_results.size - pre_weekly.weekly_results.size
    #up主数
    results[:current_up_pig_count] = Author.count
    #当前期4、3、2、1分数
    scores = [current_weekly.weekly_results.detect{|r| r.score_rank == 4}.score,
              current_weekly.weekly_results.detect{|r| r.score_rank == 3}.score,
              current_weekly.weekly_results.detect{|r| r.score_rank == 2}.score,
              current_weekly.weekly_results.detect{|r| r.score_rank == 1}.score]
    results[:four_three] = scores[1] - scores[0]
    results[:three_two] = scores[2] - scores[1]
    results[:two_one] = scores[3] - scores[2]
    #各大版块得分
    results[:anime] = WeeklyResult.sum("score", :include => :weekly_result_work,  :conditions => "weekly_id = #{current_weekly.index} and wtype in ('MAD·AMV','MMD·3D','二次元鬼畜','期刊·其他')")
    results[:music] = WeeklyResult.sum("score", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('音乐视频','三次元音乐','VOCALOID相关','翻唱')")
    results[:ent] = WeeklyResult.sum("score", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('生活娱乐','舞蹈','三次元鬼畜','影视')")
    results[:xinfan] = WeeklyResult.sum("score", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('新番二次元','新番三次元')")
    results[:game] = WeeklyResult.sum("score", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('游戏视频','游戏攻略·解说','Mugen')")
    results[:zj] = WeeklyResult.sum("score", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('專輯二次元','專輯三次元')")
    results[:technology] = WeeklyResult.sum("score", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('科普知识','人文地理','全球科技','野生技术')")
    # 各大版块硬币获得数
    results[:yb] = {}
    results[:yb][:anime] = WeeklyResult.sum("yb", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('MAD·AMV','MMD·3D','二次元鬼畜','期刊·其他')")
    results[:yb][:music] = WeeklyResult.sum("yb", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('音乐视频','三次元音乐','VOCALOID相关','翻唱')")
    results[:yb][:ent] = WeeklyResult.sum("yb", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('生活娱乐','舞蹈','三次元鬼畜','影视')")
    results[:yb][:xinfan] = WeeklyResult.sum("yb", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('新番二次元','新番三次元')")
    results[:yb][:game] = WeeklyResult.sum("yb", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('游戏视频','游戏攻略·解说','Mugen')")
    results[:yb][:zj] = WeeklyResult.sum("yb", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('專輯二次元','專輯三次元')")
    results[:yb][:technology] = WeeklyResult.sum("yb", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('科普知识','人文地理','全球科技','野生技术')")
    #各大板块推荐总分
    results[:tj] = {}
    results[:tj][:anime] = WeeklyResult.sum("tj", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('MAD·AMV','MMD·3D','二次元鬼畜','期刊·其他')")
    results[:tj][:music] = WeeklyResult.sum("tj", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('音乐视频','三次元音乐','VOCALOID相关','翻唱')")
    results[:tj][:ent] = WeeklyResult.sum("tj", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('生活娱乐','舞蹈','三次元鬼畜','影视')")
    results[:tj][:xinfan] = WeeklyResult.sum("tj", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('新番二次元','新番三次元')")
    results[:tj][:game] = WeeklyResult.sum("tj", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('游戏视频','游戏攻略·解说','Mugen')")
    results[:tj][:zj] = WeeklyResult.sum("tj", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('專輯二次元','專輯三次元')")
    results[:tj][:technology] = WeeklyResult.sum("tj", :include => :weekly_result_work, :conditions => "weekly_id = #{current_weekly.index} and wtype in ('科普知识','人文地理','全球科技','野生技术')")
    File.open(%[#{RAILS_ROOT}/config/_stat.yml],"w"){ |io| YAML.dump results,io }
    puts "#{Time.now - time} second! all complete!!"
  end
end


